<template>
  <confirm-dialog
    :title="title"
    :visible="visible"
    :loading="loading"
    @handleClose="handleClose"
    width="25%"
  >
    <template slot="content">
      <p>{{ content }}</p>
      <ul v-if="Array.isArray(data) && action !== 'delete'">
        <li v-for="(item, index) in data" :key="index">{{ item.order_id }}</li>
      </ul>
      <ul v-if="!Array.isArray(data) && action !== 'delete'">
        <li>{{ data.order_id }}</li>
      </ul>
      <div v-if="action === 'retry'"
        style="font-size: 12px; color: #333; line-height: 1.5;"
      >
        <span>{{$t('order.retryPoint')}}</span><br /><span
          >{{$t('order.point1')}}</span
        ><br /><span
          >{{$t('order.point2')}}</span
        ><br /><span style="display:inline-block;padding-left:5px"
          >{{$t('order.pointA')}}</span
        ><br /><span style="display:inline-block;padding-left:5px"
          >{{$t('order.pointB')}}</span
        ><br /><span style="display:inline-block;padding-left:5px">{{$t('order.pointC')}}</span>
      </div>
    </template>
    <template slot="footer">
      <el-button @click="handleClose">{{ $t("common.cancel") }}</el-button>
      <el-button :disabled="loading" type="primary" @click="submit">{{
        $t("common.confirm")
      }}</el-button>
    </template>
  </confirm-dialog>
</template>
<script lang="ts">
import Vue from "vue";
import FormDialop from "../../../components/Dialog/formDialog.vue";
import ConfirmDialog from "../../../components/Dialog/confirmDialog.vue";
import orinusApi from '../../../api/orinusApi';
export default Vue.extend({
  components: {
    FormDialop,
    ConfirmDialog,
  },
  props: {
    submitMethod: {
      type: Function,
      default() {
        return (formData: any) => Promise.resolve();
      },
    },
  },
  data() {
    return {
      title: "",
      visible: false,
      loading: false,
      content: "",
      data: {},
      action: "",
    };
  },
  methods: {
    handleClose() {
      this.visible = false;
      this.loading = false;
    },
    async init(obj: any) {
      // console.log(obj);
      this.data = obj.data as any;
      this.action = obj.type;
      switch(obj.type) {
        case 'retry':
          this.title = (this as any).$t('order.retry').toString();
          if (obj.data.length > 1) {
            this.content = (this as any).$t('order.batchRetryOrder').toString();
          } else {
            this.content = (this as any).$t('order.retryOrder').toString();
          };
        break;
        case 'confirmFail':
          this.title = (this as any).$t('order.confirmFail').toString();
          if (obj.data.length > 1) {
            this.content = (this as any).$t('order.batchConfirmFailOrder').toString();
          } else {
            this.content = (this as any).$t('order.confirmFailOrder').toString();
          };
        break;
        case 'delete':
          if (obj.data.length > 1) {
            this.title = (this as any).$t('common.BatchDelete').toString();
            this.content = (this as any).$t('order.batchDeleteOrder').toString();
          } else {
            this.title = (this as any).$t('common.delete').toString();
            this.content = (this as any).$t('order.deleteOrder').toString();
          };
      }
      this.visible = true;
    },
    submit() {
      this.loading = true;
      let promiseArray: any = [];
      if (Array.isArray(this.data)) {
        if (this.action === 'retry') {
          // console.log('retry');
          (this.data as any).forEach((item: any) => {
            promiseArray.push(
              (this as any).$orinusApi.orderRetry(item.order_id)
            );
          });
        } else if (this.action === 'confirmFail') {
          // console.log('confirm');
          (this.data as any).forEach((item: any) => {
            promiseArray.push(
              (this as any).$orinusApi.orderFail(item.order_id)
            );
          });
        } else {
          // console.log('delete');
          (this.data as any).forEach((item: any) => {
            promiseArray.push(
              (this as any).$orinusApi.deleteOrderDesktop(item.id)
            );
          });
        }
      } else {
        if (this.action === 'retry') {
          promiseArray.push(
            (this as any).$orinusApi.orderRetry((this.data as any).order_id)
          );
        } else if (this.action === 'confirmFail') {
          // console.log('confirm');
          promiseArray.push(
            (this as any).$orinusApi.orderFail((this.data as any).order_id)
          );
        } else {
          // console.log('delete');
          promiseArray.push(
            (this as any).$orinusApi.deleteOrderDesktop((this.data as any).id)
          );
        }
      }
      Promise.all(promiseArray)
        .then((res) => {
          this.loading = false;
          this.visible = false;
          this.$emit("success");
          this.$message({
            message: this.$t("message.requestSuccess").toString(),
            type: "success",
          });
        })
        .catch(() => {
          this.loading = false;
          this.$message({
            message: this.$t("message.actionError").toString(),
            type: "warning",
          });
        });
    },
  },
});
</script>
